package com.yuanduo_app.route;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.chenenyu.router.IRouter;
import com.chenenyu.router.Router;
import com.yuanduo_app.MainApplication;
import com.xujl.fastlib.utils.LogS;
import com.xujl.fastlib.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 路由跳转工具类
 */
public class ARouterUtils {

    public static void startActivity (String routerUrl, Map<String, Object> params) {
        if (StringUtils.isEmpty(routerUrl)) {
            return;
        }
        String routerPath = getRouterPath(routerUrl);

        LogS.i("ARouterUtils", routerPath);
        final IRouter build = Router.build(routerPath);
        //传递参数
        if (params != null) {
            for (String key : params.keySet()) {
                final Object value = params.get(key);
                build.with(key, value);
            }
        }
        build.go(MainApplication.getInstance());
    }


    public static void startActivityWithBundle (String routerUrl, Bundle bundle) {
        Router.build(routerUrl)
                .with(bundle)
                .go(MainApplication.getInstance());
    }

    public static Bundle createBundleWithRoute (String routerUrl) {
        final Bundle bundle = new Bundle();
        final Map<String, Object> params = getRouterUrlParams(routerUrl);
        if (params == null) {
            return bundle;
        }
        for (String key : params.keySet()) {
            final Object value = params.get(key);
            bundle.putString(key, (String) value);
        }
        return bundle;
    }

    /**
     * 无参跳转
     *
     * @param routerUrl
     */
    public static void startActivity (String routerUrl) {
        startActivity(routerUrl, getRouterUrlParams(routerUrl));
    }

    /**
     * 获取路由参数map
     *
     * @param routerUrl
     * @return
     */
    public static Map<String, Object> getRouterUrlParams (String routerUrl) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(routerUrl)) {
            return map;
        }
        int start = routerUrl.indexOf("?");
        if (start >= 0) {
            String str = routerUrl.substring(start + 1);
            String[] paramsArr = str.split("&");
            String[] temp = null;
            for (String param : paramsArr) {
                temp = param.split("=");
                map.put(temp[0], temp.length < 2 ? "" : temp[1]);
            }
        }
        if (map.containsKey("url") && map.get("url") != null) {
            try {
                map.put("url", URLDecoder.decode(map.get("url").toString(), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /**
     * 解析路由路径
     *
     * @return
     */
    public static String getRouterPath (String routerUrl) {
        if (StringUtils.isEmpty(routerUrl)) {
            return "";
        }
        final int indexOf = routerUrl.indexOf("?");
        String resultUrl = routerUrl;
        //去除参数后缀
        if (indexOf != -1) {
            resultUrl = routerUrl.substring(0, indexOf);
        }
        //去除无用前缀
        return resultUrl.replace(RouteName.BASE_ROUTE, "");
    }

    /**
     * 返回单个字符串，若匹配到多个的话就返回第一个
     *
     * @param soap
     * @param rgex
     * @return
     */
    public static String getSubUtilSimple (String soap, String rgex) {
        Pattern pattern = Pattern.compile(rgex);// 匹配的模式
        Matcher m = pattern.matcher(soap);
        while (m.find()) {
            return m.group(1);
        }
        return "";
    }

    /**
     * 根据路由获取act类类型
     *
     * @param routerPath
     * @return
     */
    public static Class getActivityCls (String routerPath, Context context) {
        final Intent intent = Router.build(routerPath).getIntent(context);
        try {
            return Class.forName(intent.getComponent().getClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
